package com.k8.rpc.url;

import com.k8.common.util.StrUtil;
import com.k8.rpc.util.URLUtils;
import lombok.Getter;
import lombok.Setter;

public class URLAddress {
    @Getter
    @Setter
    private String host = "localhost";
    @Setter
    @Getter
    private int port;
    @Getter
    private String path = "/";//如果是protocolUrl，path一般由 /服务接口全限定名:group:version（不使用实现类的全限定名）

    public void setPath(String path) {
        if (!path.startsWith("/")) path = "/" + path;
        this.path = path;
    }

    //protected transient long timestamp;
    @Override
    public String toString() {
        String tempHostS = host;
        if (StrUtil.isEmpty(tempHostS)) {
            tempHostS = "localhost";
        }
        String ip = URLUtils.getIp(tempHostS);
        String tempPath = path;
        if (StrUtil.isEmpty(tempPath)) tempPath = "";
        return ip + ":" + port + path;
    }

    /**
     *
     */
    public String getIp() {
        return URLUtils.getIp(host);
    }

    public String getAddr() {
        return getIp() + ":" + port;
    }
}
